import pymysql
from pymysql import Error

from utils.config import load_config


def create_connection():
    config = load_config()
    """创建数据库连接"""
    connection = None
    try:
        connection = pymysql.connect(
            host=config['mysql']['host'],
            user=config['mysql']['user'],
            password=config['mysql']['password'],
            database=config['mysql']['database']
        )
        print("连接 MySQL 数据库成功")
    except Error as e:
        print(f"连接数据库时发生错误: {e}")
    return connection
